<?php
namespace app\admin\controller;

use app\common\controller\SysAction;

class Comments extends SysAction
{
    protected $modelClass = '\app\common\model\GoodsComments';
    
    public function initialize()
    {
        parent::initialize();
    }
    
    public function index(){
    	$where[] = ['is_one', '=', 1];
        if (input('get._sort')){
            $order = explode(',', input('get._sort'));
            $order = $order[0].' '.$order[1];
        }else{
            $order = 'create_time asc';
        }
        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());
        foreach ($dataList as $key => $value) {
        	$comms = $this->cModel->where("order_id",$value['order_id'])->select();
        	$data = $this->commentsort($comms);
        }
        $this->assign("data",$data);
        $this->assign('dataList', $dataList);
        return $this->fetch();
    }

    public function commentsort($data,$pid=0,$level=0){
    	//此处数据必须是静态数组，不然递归的时候每次都会声明一个新的数组
       	static $arr=array();
        foreach ($data as $key=>$value){
            if($value['pid'] == $pid){
                $value["level"]=$level;
                $arr[]=$value;
                $this->commentsort($data,$value['id'],$level+1);
            }
        }
        return $arr;
    }

    public function get_str($id = 0, $level = 0) {
        $flag = '';
        $str = '';
        $result = $this->cModel->where('order_id',$id)->select();
        if ($result) {
        	foreach ($result as $k => $v) {
        		$str .= '<tr>';
        		$str .= '<td><input type="checkbox" name="id[]" value="'.$v['id'].'" class="minimal"></td>';
        		$str .= '<td>'.├.'</td>';
        		$str .= '<td></td>';
        		$str .= '<td></td>';
        		$str .= '<td>'.$v['content'].'</td>';
        		$str .= '<td></td>';
        		$str .= '<td>'.$v['create_time'].'</td>';
        		if ($v['mid']) $people = $v['mid'];
        		if ($v['agent_id']) $people = $v['agent_id'];
        		if ($v['uid']) $people = $v['uid'];
        		$str .= '<td>'.$people.'</td>';
        		$str .= '<td><a class="btn btn-primary btn-xs" href="'.url('reply',['id'=>$v['id']]).'"><i class="fa fa-edit"></i> 回复</a> <a class="btn btn-danger btn-xs delete" href="javascript:void(0);" data-url="/admin/comments/delete" data-id="5" data-index="data-title=&quot;删除&quot;"><i class="fa fa-trash"></i> 删除</a> </td>';
        		$str .= '</tr>';
        	}
        }
        // $sql = "select * from category where pid= $id"; 
        // $result = mysql_query($sql);

        if($result && mysql_affected_rows()){

            if( ! $flag) {
                $flag = 1;
            } else {
                $str .= '<div class="comments-list">';
            }

            while ($row = mysql_fetch_array($result)) {
                $str .= '<div class="comment"><a href="###" class="avatar">';
                $str .= '<i class="icon-camera-retro icon-2x"></i></a><div class="content">';
                $str .= '<div class="pull-right text-muted">30 分钟前</div><div><a href="###">';
                $str .= '<strong>第一</strong></a> <span class="text-muted">回复</span><a href="###">某某某</a></div>';

                $str .= '<div class="text">' . $row['id'] . "--" . $row['catename'] . '</div>';
                 
                $str .= "<div class='actions'> <a href=''#>回复</a></div></div>";

                get_str($row['id'], $level+1);
            }
            $str .= '</div>';
        }

        $str .= '</div>';

        return $str;
    }

}